/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.frontends.owlxml;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Laurent WOUTERS
 */
public class OntologyLoader {
    private static final String ontologyAttrIRI = "ontologyIRI";

    private xowl.interpreter.frontends.OntologyListener listener;
    private xowl.lang.owl2.Ontology ontology;
    private Map<String, String> mapPrefixes;
    private Map<String, xowl.lang.owl2.AnonymousIndividual> mapAnonIndividuals;
    
    public OntologyLoader(xowl.interpreter.frontends.OntologyListener listener) {
        this.listener = listener;
        this.mapPrefixes = new HashMap<String, String>();
        this.mapAnonIndividuals = new HashMap<String, xowl.lang.owl2.AnonymousIndividual>();
    }

    public xowl.lang.owl2.Ontology load(org.w3c.dom.Node node) {
        String iri = node.getAttributes().getNamedItem(ontologyAttrIRI).getNodeValue();
        ontology = listener.resolveOntology(listener.getIRI(iri));
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        for (org.w3c.dom.Node child : children) {
            String name = child.getNodeName();
            if (name.equals(xowl.interpreter.frontends.VocOWL2.ontoPrefix))
                load_Prefix(child);
            else if (name.equals(xowl.interpreter.frontends.VocOWL2.ontoImport))
                load_Import(child);
            else if (name.equals(xowl.interpreter.frontends.VocOWL2.ontoAnnotation))
                ontology.addAnnotations(AnnotationLoader.load(this, child, new xowl.interpreter.frontends.LexicalContext()));
            else if (xowl.interpreter.frontends.VocRules.ruleRule.equals(name)) {
                // Treat rule
                xowl.lang.rules.Rule rule = RuleLoader.load(this, child, new xowl.interpreter.frontends.LexicalContext());
                if (rule != null)
                    listener.onRule(ontology, rule);
            } else {
                xowl.lang.owl2.Axiom axiom = AxiomLoader.load(this, child, new xowl.interpreter.frontends.LexicalContext());
                if (axiom != null)
                    listener.onAxiom(ontology, axiom);
            }
        }
        return ontology;
    }

    private void load_Prefix(org.w3c.dom.Node node) {
        String name = node.getAttributes().getNamedItem("name").getNodeValue();
        String iri = node.getAttributes().getNamedItem("IRI").getNodeValue();
        mapPrefixes.put(name, iri);
    }
    private void load_Import(org.w3c.dom.Node node) { }
    
    public xowl.lang.owl2.IRI getFullIRI(org.w3c.dom.Node node) {
        org.w3c.dom.Node attr = node.getAttributes().getNamedItem("IRI");
        if (attr != null) return getFullIRI(attr.getNodeValue());
        attr = node.getAttributes().getNamedItem("abbreviatedIRI");
        if (attr != null) return getFullIRI(attr.getNodeValue());
        return null;
    }
    public xowl.lang.owl2.IRI getFullIRI(String name) {
        if (name.startsWith("#"))
            return listener.getIRI(ontology.getHasIRI().getHasValue() + name);
        else if (name.contains("#"))
            return listener.getIRI(name);
        else if (name.contains(":")) {
            String parts[] = name.split(":");
            return listener.getIRI(mapPrefixes.get(parts[0]) + parts[1]);
        } else
            return listener.getIRI(ontology.getHasIRI().getHasValue() + "#" + name);
    }

    public xowl.lang.owl2.AnonymousIndividual resolveAnonymousIndividual(String id) {
        if (mapAnonIndividuals.containsKey(id))
            return mapAnonIndividuals.get(id);
        xowl.lang.owl2.AnonymousIndividual ind = new xowl.lang.owl2.AnonymousIndividual();
        ind.setNodeID(id);
        mapAnonIndividuals.put(id, ind);
        return ind;
    }
}
